<template>
  <div class="product-detail">
    <Header title="详情页面" />
    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <van-swipe class="my-swipe" indicator-color="#1baeae">
          <van-swipe-item v-for="(item, index) in detailData.goodsCarouselList" :key="index">
            <img :src="item" alt="item" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="product-info">
        <div class="product-title">
          {{ detailData.goodsName || "" }}
        </div>
        <div class="product-desc">免邮费 顺丰快递</div>
        <div class="product-price">
          <span>¥{{ detailData.sellingPrice || "" }}</span>
        </div>
      </div>
      <div class="product-intro">
        <ul>
          <li>概述</li>
          <li>参数</li>
          <li>安装服务</li>
          <li>常见问题</li>
        </ul>
        <div class="product-content" v-html="detailData.goodsDetailContent || ''"></div>
      </div>
    </div>
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" color="#ee0a24" />
      <van-action-bar-icon icon="cart-o" to="/cart" text="购物车" :badge="store.totalCartNum" />
      <!-- <van-action-bar-icon icon="star" text="已收藏" color="#ff5000" /> -->
      <van-action-bar-button type="warning" text="加入购物车" @click="addToCart" />
      <van-action-bar-button type="danger" text="立即购买" />
    </van-action-bar>
  </div>
</template>

<script setup>
  import { getDetail, addCart } from "@/api/detail"
  import { reactive, onMounted } from "vue"
  import { useRoute } from "vue-router"
  import Header from "@/components/Header.vue"
  import { showSuccessToast, showFailToast } from "vant"
  import { useCartStore } from "@/store/cartStore"
  let store = useCartStore()
  //获取页面传递的商品id
  let route = useRoute()
  let id = route.params.id
  let detailData = reactive({})
  onMounted(() => {
    //初始化购物车数据
    store.getCartInfo()
    getDetailData()
  })
  let getDetailData = async () => {
    let { data, resultCode } = await getDetail(id)
    if (resultCode === 200) {
      for (const key in data) {
        detailData[key] = data[key]
      }
    }
  }
  let addToCart = async () => {
    let { resultCode, message } = await addCart({ goodsCount: 1, goodsId: id })
    if (resultCode === 200) {
      showSuccessToast("加入购物车成功")
      //加入成功后需要更新store中购物车的数据进行反显
      store.getCartInfo()
    } else {
      showFailToast(message || "加入购物车失败")
    }
  }
</script>

<style scoped lang="less">
  @import "@/assets/css/public";
  .product-detail {
    .detail-header {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 10000;
      .fj();
      .wh(100%, 44px);
      line-height: 44px;
      padding: 0 10px;
      .boxSizing();
      color: #252525;
      background: #fff;
      border-bottom: 1px solid #dcdcdc;
      .product-name {
        font-size: 14px;
      }
    }
    .detail-content {
      height: calc(100vh - 50px);
      overflow: hidden;
      overflow-y: auto;
      .detail-swipe-wrap {
        .my-swipe .van-swipe-item {
          img {
            width: 100%;
          }
        }
      }
      .product-info {
        padding: 0 10px;
        .product-title {
          font-size: 18px;
          text-align: left;
          color: #333;
        }
        .product-desc {
          font-size: 14px;
          text-align: left;
          color: #999;
          padding: 5px 0;
        }
        .product-price {
          .fj();
          span:nth-child(1) {
            color: #f63515;
            font-size: 22px;
          }
          span:nth-child(2) {
            color: #999;
            font-size: 16px;
          }
        }
      }
      .product-intro {
        width: 100%;
        padding-bottom: 50px;
        ul {
          .fj();
          width: 100%;
          margin: 10px 0;
          li {
            flex: 1;
            padding: 5px 0;
            text-align: center;
            font-size: 15px;
            border-right: 1px solid #999;
            box-sizing: border-box;
            &:last-child {
              border-right: none;
            }
          }
        }
        .product-content {
          padding: 0 20px;
          img {
            width: 100%;
          }
        }
      }
    }
    .van-action-bar-button--warning {
      background: linear-gradient(to right, #6bd8d8, @primary);
    }
    .van-action-bar-button--danger {
      background: linear-gradient(to right, #0dc3c3, #098888);
    }
  }
</style>
